package ip.command;
/***********************************************************************
 * Module:  UpdateCategoryCommand.java
 * Author:  mojsilo
 * Purpose: Defines the Class UpdateCategoryCommand
 ***********************************************************************/

import ip.dto.CategoryDTO;
import ip.ejb.session.facade.CategoryFacadeLocal;
import ip.ejb.session.facade.CategoryFacadeLocalHome;
import ip.exceptions.NoSuchCategoryException;
import ip.exceptions.UnexpectedException;
import ip.util.EJBHomeFactory;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;
import javax.servlet.ServletException;

import org.apache.log4j.Logger;

public class UpdateCategoryCommand extends Command
{
	private CategoryDTO categoryDTO;
	private CategoryFacadeLocal categoryFacadeLocal;
	private Logger logger = Logger.getLogger(this.getClass());

	public void init() throws UnexpectedException{
		try {
			logger.debug("init()");
			CategoryFacadeLocalHome categoryFacadeLocalHome = (CategoryFacadeLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.session.facade.CategoryFacadeLocalHome.class);
			categoryFacadeLocal = categoryFacadeLocalHome.create();

		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();			
		} catch (CreateException e) {
			logger.error(e);
			e.printStackTrace();			
			throw new UnexpectedException();			
		}

	}

	public void execute() throws UnexpectedException, NoSuchCategoryException{
		try {
			logger.debug("execute()");
			categoryFacadeLocal.updateCategory(categoryDTO);
		} catch (NoSuchCategoryException e) {			
			e.printStackTrace();
			throw new NoSuchCategoryException(categoryDTO);			
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();			
			throw new UnexpectedException();			
		}
	}

	public void dispose() throws UnexpectedException{
		try {
			logger.debug("dispose()");
			categoryFacadeLocal.remove();

		} catch (EJBException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (RemoveException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		}
	}

	public void setCategoryDTO(CategoryDTO categoryDTO)
	{
		this.categoryDTO = categoryDTO;
	}

}